package org.itboys.mobile.service.mysql.mobile.product;

import org.itboys.admin.dto.lasted.SundayUploadDto;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mdl.conmon.Finals;
import org.itboys.mdl.dto.comment.MdlCommentDto;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.product.SundayProductDto;
import org.itboys.mobile.entity.mongo.cart.MdlCart;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.entity.mysql.product.SundayProductCategory;
import org.itboys.mobile.service.mongo.mobile.cart.MdlMobileCartService;
import org.itboys.mobile.service.mongo.mobile.collection.MdlMobileCollectionService;
import org.itboys.mobile.service.mongo.mobile.comment.MdlMobileCommentService;
import org.itboys.mobile.service.mysql.web.product.SundayProductCategoryService;
import org.itboys.mongodb.service.BaseMySqlService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/12/1
 * 描述：迈迪龙_手机端_产品
 */
@Service
public class SundayMobileProductService extends BaseMySqlService<SundayProduct>{
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private MdlMobileCollectionService collectionService;
    @Autowired
    private MdlMobileCommentService commentService;
    @Autowired
    private SundayProductCategoryService categoryService;
    @Autowired
    private MdlMobileCartService cartService;
    /**
     * 查询
     * @param request
     * @return
     */
    public List<SundayProductDto> selectProduct(HttpServletRequest request,Long memberId){
        Map<String,Object> param= ParamUtil.packageMysqlPageParam(request,"type","categoryId","name");
        List<SundayProductDto> productDtos= packageDtoMuilt(super.select(param));
        Map<String,Object> cartParam=new HashMap<String,Object>();
        cartParam.put("isDeleted", SundayCommonConstants.NO);
        cartParam.put("memberId",memberId);
        List<MdlCart> carts=cartService.list(cartParam);
        //根据用户之前的购物车   在产品中加入之前购物车产品数量
        if(ListUtil.isNotNull(productDtos)){
            for(SundayProductDto productDto:productDtos){
                Integer cartNumber=0;
                if(ListUtil.isNotNull(carts)){

                    for(MdlCart cart:carts){
                        if(productDto.getId()==cart.getProductId().longValue()){
                            cartNumber+=cart.getNum();
                        }
                    }
                }
                productDto.setCartNumber(cartNumber);
            }
        }

        return productDtos;

    }

    /**
     * 获取商品分类
     * @param request
     * @return
     */
    public List<SundayProductCategory> selectCategory(HttpServletRequest request){
        Map<String,Object> param= ParamUtil.packageMysqlPageParam(request,"type","name");
        return categoryService.select(param);
    }


    /**
     * 查找单个
     * @param memberId
     * @param productId
     * @return
     */
    public SundayProductDto findOne(Long memberId, Long productId){
        SundayProduct product =super.find(productId);
        //推荐商品
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("categoryId",product.getCategoryId());
        param.put("status",1);
        param.put("row_start",0);
        param.put("row_size",2);
        param.put("type",product.getType());
        List<SundayProduct> recProducts=super.select(param);
        List<Map<String,Object>> recommendProducts = new ArrayList<>();
        for(SundayProduct productDto:recProducts){
            Map<String,Object> reProduct = new HashMap<String,Object>();
            reProduct.put("id",productDto.getId());
            reProduct.put("name",productDto.getName());
            reProduct.put("viceName",productDto.getViceName());
            reProduct.put("image",productDto.getImage());
            reProduct.put("price",productDto.getPrice());
            reProduct.put("marketPrice",productDto.getMarketPrice());
            recommendProducts.add(reProduct);
        }
        Map<String,Object> cartParam=new HashMap<String,Object>();
        cartParam.put("isDeleted", SundayCommonConstants.NO);
        cartParam.put("memberId",memberId);
        List<MdlCart> carts=cartService.list(cartParam);
        Integer cartNumber=0;
        if(ListUtil.isNotNull(carts)){
            for(MdlCart cart:carts) {
                if (product.getId() == cart.getProductId().longValue()) {
                    cartNumber += cart.getNum();
                }
            }
        }
        product.setCartNumber(cartNumber);
        return packageDtoSingle(product,memberId,recommendProducts);

    }

    public SundayProductDto packageDtoSingle(SundayProduct product, Long memberId,List<Map<String,Object>> recommendProducts){

        SundayProductDto dto = null;
        //静态资源


        //组装轮播图
        List<SundayUploadDto> rollImages = uploadService.selectNoPage(product.getId(),SundayProduct.class.getSimpleName());
        //商品详情URL
        String descUrl =resourceHolder.getStringValue("mobileRoot")+"/sunday/mobile/product/desc?id="+product.getId();
        //判断受否收藏
        Integer isCollection =collectionService.checkIsCollection(memberId, Finals.collection_type_product,product.getId());
        //加载默认评论
        logger.info("加载评论");
        List<MdlCommentDto> comments = commentService.getCommentByNumber(2,product.getId(),SundayProduct.class.getSimpleName());
        logger.info("加载评论-结束");
        //加载推荐商品

        dto=  new SundayProductDto(product.getId(), product.getCreateTime(), product.getCreateDate(),
                product.getName()+product.getStandard(), product.getNumber(), product.getViceName(),   product.getStandard(),  product.getMarketPrice(), product.getPrice(),  product.getType(),
                product.getImage(),  product.getSaleNumber(), product.getCartNumber(),rollImages,
                isCollection,  descUrl,  recommendProducts, comments,product.getStore());


        return dto;
    }


    /**
     * 组装商品DTO
     * @param products
     * @return
     */
    public List<SundayProductDto> packageDtoMuilt(List<SundayProduct> products){
        String imageRoot = resourceHolder.getStringValue("imgRoot");
        List<SundayProductDto> dtos = new ArrayList<SundayProductDto>();
        for(SundayProduct product:products){
            //主图
            dtos.add(
                    new SundayProductDto(product.getId(), product.getCreateTime(), product.getCreateDate(),
                            product.getName()+product.getStandard(),  product.getViceName(),   product.getStandard(),  product.getMarketPrice(), product.getPrice(),  product.getType(),
                            imageRoot+product.getImage(),  product.getSaleNumber(),  product.getStore(),product.getNumber()));

        }
        return dtos;
    }

}
